package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.mapper.WarningInfoMapper;
import com.example.demo.entity.Message;
import com.example.demo.entity.WarningInfos;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
public class UpdateWarningController {
    @Autowired
    private  WarningInfoMapper wm;
    @RequestMapping("/updatewarning")
    public void updateWarning(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int warningid=Integer.valueOf(request.getParameter("warningid"));
        int warningtype=Integer.valueOf(request.getParameter("warningtype"));
        String warningdetail=request.getParameter("warningdetail");
        Date date = new Date(System.currentTimeMillis());
        int n=wm.updateWarningInfo(warningtype,warningdetail,date,warningid);
        Map<String,Date> time=new HashMap<>();
        WarningInfos warningInfos = wm.selectByWarningId(warningid);
        time.put("createtime",warningInfos.getCreatetime());
        time.put("updatetime",date);
        if(n>0){
            response.getWriter().write(JSONObject.toJSONString(Message.success(time)));
        }else response.getWriter().write(JSONObject.toJSONString(Message.failure("Warninginfo update failure")));
    }
}
